package com.gua.jbs.controller;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.gua.jbs.bean.CouponBean;
import com.gua.jbs.service.ICouponService;
import com.gua.jbs.serviceImpl.CouponService;
import com.gua.jbs.utility.ApplicationConstant;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.DBConstant;
import com.gua.jbs.utility.StringUtil;



/**
 * Controller related to the addition of a new Coupon
 * in the system, edit and delete functionality
 * @author Yogesh
 *
 */

public class CouponController extends BaseController {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger
			.getLogger(LoginController.class);
	

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String msgCoupon="";
		ICouponService couponService = new CouponService();
		// Get the action parameter to understand the type of request
		String action = request.getParameter(ApplicationConstant.ACTION);
		
		String couponName = request.getParameter(ApplicationConstant.NAME_CP);
		//String couponCode = request.getParameter(ApplicationConstant.SAVE_COUPON);
		String couponFormulae = request.getParameter(ApplicationConstant.FORMULAE_CP);
		String couponTypes = request.getParameter(ApplicationConstant.TYPES_CP);
		//String couponApprove=request.getParameter(ApplicationConstant.couponApprove);
		
		if(StringUtil.isNullOrEmpty(action) && action.equals(ApplicationConstant.ADD_COUPON)){
			CouponBean couponBean=new CouponBean();
			couponBean.setName(couponName);
			couponBean.setGroup(ApplicationConstant.SAVE_COUPON);
			couponBean.setFormulae(couponFormulae);
			couponBean.setType(couponTypes);
			boolean isApproved=true;
			/*if(couponApprove.equals(DBConstant.IS_ACTIVE_NO)){
				isApproved=false;
			}
			couponBean.setApproved(isApproved);
			*/
			boolean isAdded=false;
			
			try {
				isAdded = couponService.addCoupon(couponBean);
				if(isAdded){
					msgCoupon=ApplicationConstant.successCouponMsg;
					request.setAttribute("msgCoupon", msgCoupon);
				}else{
					msgCoupon=ApplicationConstant.failureCouponMsg;
				}
			} catch (ApplicationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			response.sendRedirect("/jewel/pages/jsp/newCoupon.jsp?msgCoupon="+msgCoupon);
		 }
		else{
			Map<Integer, Map<String, String>> map_couponType = new LinkedHashMap<Integer, Map<String, String>>();
			try {
				map_couponType = couponService.getCouponType();
				request.setAttribute("couponType", map_couponType);

			} catch (ApplicationException e) {
				// TODO Auto-generated catch block
				msgCoupon=e.getMessage();
				e.printStackTrace();
			}
			//.sendRedirect("/jewel/pages/jsp/newCoupon.jsp?msgCoupon="+msgCoupon);
			rd = request.getRequestDispatcher("/pages/jsp/newCoupon.jsp");
			rd.forward(request, response);
		 }

	}

}
